package com.jrapider.designer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jrapider.compent.dict.DictData;
import com.jrapider.compent.dict.IDictProvider;
import com.jrapider.mapper.SysEntityMapper;
import com.jrapider.om.PageInfo;
import com.jrapider.om.SysEntity;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/17.
 */
@Service
public class SysEntityService extends ServiceImpl<SysEntityMapper, SysEntity> implements IDictProvider {

    public Long saveAndGetId(String tableName) {
        SysEntity condition = new SysEntity();
        condition.setTableName(tableName);
        SysEntity data = baseMapper.selectOne(new QueryWrapper<>(condition));
        if (data == null) {
            condition.setEntityName(StringUtils.underlineToCamel(condition.getTableName()));
            baseMapper.insert(condition);
            return condition.getId();
        } else {

        }
        return data.getId();
    }

    @Override
    public Set<String> supportCodes() {
        return new HashSet<String>() {{
            add("entity");
        }};
    }

    @Override
    public List<DictData> getDict(String code) {
        List<SysEntity> list = baseMapper.selectList(new QueryWrapper<SysEntity>());
        return list.stream().map(sysEntity -> new DictData() {{
            setValue(sysEntity.getId() + "");
            setCode(code);
            setNote(sysEntity.getEntityName());
        }}).collect(Collectors.toList());
    }

    @Override
    public List<DictData> getDict(String code, String value, PageInfo pageInfo) {
        return getDict(code);
    }
}
